package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.ui.prt.PtrScrollContent;
import com.typy.www.tpay.ui.view.CircleImageView;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/18.
 */

public class ActivicationSecontActivity extends BaseBackActivity {


    @BindView(R.id.img_touxian)
    CircleImageView imgTouxian;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_uid)
    TextView tvUid;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_touxian)
    PercentLinearLayout llTouxian;
    @BindView(R.id.publish)
    TextView publish;
    UserInfoVm userInfoVm;
    @BindView(R.id.et_exchangeamount)
    AutoCompleteTextView etExchangeamount;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.et_fenxiangfen)
    AutoCompleteTextView etFenxiangfen;
    @BindView(R.id.tv_turntitle)
    TextView tvTurntitle;
    private String phone;
    PtrScrollContent ptrScrollContent;
    private int classType;
    View item_turnout;

    public static void show(Context context, String data, String phone, int classType) {
        Intent intent = new Intent();
        intent.putExtra("data", data);
        intent.putExtra("phone", phone);
        intent.putExtra("classType", classType);
        IntentUtil.intent2Activity(context, intent, ActivicationSecontActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        phone = getIntent().getStringExtra("phone");
        classType = getIntent().getIntExtra("classType", 0);
        initTitle();
        String data = getIntent().getStringExtra("data");
        if (!StringUtil.isEmpty(data)) {
            updataUserInfo(getIntent().getStringExtra("data"));
        } else {
            getUserInfo();
        }
    }

    private void initTitle() {
        setTitle(ClassHoldUtil.getClassTitle(classType));
        mBtnRight.setVisibility(View.VISIBLE);
        if (classType == ClassHoldUtil.Activication) {
            ((TextView) mBtnRight).setText("激活记录");
            publish.setText("确认激活");
            findViewById(R.id.item_turnout).setVisibility(View.VISIBLE);
            findViewById(R.id.item_turnoutpoint).setVisibility(View.VISIBLE);
        } else if (classType == ClassHoldUtil.TurnOut) {
            ((TextView) mBtnRight).setText("转出记录");
            publish.setText("确认转出");
            findViewById(R.id.item_turnout).setVisibility(View.VISIBLE);
            findViewById(R.id.item_turnoutpoint).setVisibility(View.VISIBLE);

        } else if (classType == ClassHoldUtil.TurnOutDigi) {
            ((TextView) mBtnRight).setText("转出记录");
            publish.setText("确认转出");
            tvTurntitle.setText("Zpay数量");
            etExchangeamount.setHint("请输入转出数量");
            findViewById(R.id.item_turnout).setVisibility(View.VISIBLE);
            findViewById(R.id.item_turnoutpoint).setVisibility(View.GONE);

        }

    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
        RecordActivity.show(getActivity(), ClassHoldUtil.classTurnRecord(classType));
    }

    private void updataUserInfo(String data) {
        userInfoVm = GsonUtils.parseJSON(data, UserInfoVm.class);
        if (userInfoVm != null) {
            UpdataUiUtil.getDefaul().setText(userInfoVm.getFdNickName(), tvNickname)
                    .setText("UID:" + userInfoVm.getFdId(), tvUid);
            if (classType == ClassHoldUtil.TurnOutDigi)
                UpdataUiUtil.getDefaul()
                        .setText(userInfoVm.getFdQuantity(), etExchangeamount);
        } else {
            UiHelper.showToast(getActivity(), "获取个人信息失败，请刷新重试");
        }
    }

    @Override
    protected View getContentView() {
        ptrScrollContent = new PtrScrollContent(getActivity(), R.layout.activity_activicationsecont) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                if (classType != ClassHoldUtil.TurnOutDigi)
                    getUserInfo();
                else
                    ptrScrollContent.loadInitialPage();
            }
        };
        return ptrScrollContent.getmView();
    }

    private void getUserInfo() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdCode", phone);
        HTTPUtils.post(this, Constance.getUserinfoURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (!ret.getData().toString().equals("{}")) {
                    updataUserInfo(ret.getData().toString());
                    ptrScrollContent.loadInitialPage();
                } else {
                    UiHelper.showToast(getActivity(), "未查询到该用户！");
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });

    }


    @OnClick(R.id.publish)
    public void onViewClicked() {
        if (ClickFastUtil.isFastClick500()) {
            if (userInfoVm == null) {
                UiHelper.showToast(this, "获取个人信息失败，请刷新重试");
                return;
            }
            if (classType == ClassHoldUtil.Activication) {
                if (CheckUtil.checkText(this, etExchangeamount, etFenxiangfen)) {
                    PwdDailogUtil.show(this, new InputCallBack() {
                        @Override
                        public void onInputFinish(String result) {
                            save(result);
                        }
                    });
                }
            } else if (classType == ClassHoldUtil.TurnOut) {
                if (StringUtil.isEmpty(etExchangeamount.getText().toString()) && StringUtil.isEmpty(etFenxiangfen.getText().toString())) {
                    SyDialogHelper.showAlertDlg(getActivity(), "转出余额和分享分必须填写一个");
                    return;
                } else {
                    PwdDailogUtil.show(this, new InputCallBack() {
                        @Override
                        public void onInputFinish(String result) {
                            saveTurn(result);
                        }
                    });
                }
            }else if(CheckUtil.checkText(this,etExchangeamount)){
                PwdDailogUtil.show(this, new InputCallBack() {
                    @Override
                    public void onInputFinish(String result) {
                        saveTurnDigi(result);
                    }
                });
            }
        }
    }

    private void save(String password) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdId", userInfoVm.getFdId());
        paramsUtil.put("fdAmount", etExchangeamount);
        paramsUtil.put("fdSharePoint", etFenxiangfen);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getBusinessativeURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "激活成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.classTurnRecord(classType));
                        finish();
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void saveTurn(String password) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdId", userInfoVm.getFdId());
        paramsUtil.putDefaul("fdAmount", etExchangeamount);
        paramsUtil.putDefaul("fdSharePoint", etFenxiangfen);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getTurnoutURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "转出成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.classTurnRecord(classType));
                        finish();
                    }
                });

            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }private void saveTurnDigi(String password) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdId", userInfoVm.getFdId());
        paramsUtil.putDefaul("fdQuantity", etExchangeamount);
        paramsUtil.putDefaul("fdSharePoint", etFenxiangfen);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getIcoturnoutstep2URL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "转出成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.classTurnRecord(classType));
                        finish();
                    }
                });

            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }
}
